package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.CustomerBean;
import com.project.bean.SupplierBean;
import com.project.mapper.ICustomerMapper;
import com.project.service.ICustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class CustomerServiceImpl implements ICustomerService{
    @Autowired
    private ICustomerMapper mapper;
    @Override
    public IPage<CustomerBean> findByItem(Integer pageNO, String customerName, String scope) {
        QueryWrapper<CustomerBean> qw =new QueryWrapper<>();
        if (customerName != null){
            qw.like("c_name",customerName);
        }
        if (scope != null){
            qw.like("c_scope",scope);
        }
        return mapper.selectPage(new Page(pageNO,3),qw);
    }

    @Override
    public CustomerBean findById(Integer customerId) {
        return mapper.selectById(customerId);
    }

    @Override
    public void update(Integer customerId, String headName, String headPhone, String scope) {
        CustomerBean customer = mapper.selectById(customerId);
        customer.setHeadName(headName);
        customer.setHeadPhone(headPhone);
        customer.setScope(scope);
        mapper.updateById(customer);
    }


    @Override
    public boolean add(CustomerBean customer) {
        QueryWrapper<CustomerBean> qw = new QueryWrapper<>();
        qw.eq("c_name",customer.getCustomerName());
        if (mapper.selectCount(qw)==0){
            mapper.insert(customer);
            return true;
        }
        return false;
    }

    @Override
    public List<CustomerBean> findAll() {
        return mapper.selectList(null);
    }
}
